package com.itheima.tlais_web_management.aop;

        import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.*;
        import org.springframework.stereotype.Component;

//@Aspect
@Component
public class LoggerAspect {

    // 切点：匹配所有无返回值的方法
    @Pointcut("execution(void com.itheima.tlais_web_management.service.impl.*.*(..))")
    public void voidMethods() {}
/*

    // 前置通知：在进入切点方法前打印"进入方法..."
    @Before("voidMethods()")
    public void before(JoinPoint joinPoint) {
        System.out.println("开启事务...");
    }

    // 后置通知：在切点方法执行后打印"离开方法..."
    @After("voidMethods()")
    public void after(JoinPoint joinPoint) {
        System.out.println("关闭事务...");
    }

    @AfterReturning("voidMethods()")
    public void afterreturning(JoinPoint joinPoint){System.out.println("事务提交...");}

    @AfterThrowing(pointcut = "voidMethods()",throwing = "exception")
    public void afterthrowing(JoinPoint joinPoint,Exception exception){
        System.out.println("回滚事务...");
    }
*/


    @Around("voidMethods()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("开启事务...");

        try {

            Object proceed = joinPoint.proceed();

            System.out.println("事务提交");

            return proceed;

        } catch (Exception exception) {

            System.out.println("回滚事务");

            throw exception;
        } finally {

            System.out.println("关闭事务");

        }


    }


//    @AfterThrowing(pointcut = "pt()", throwing = "exception")
//    public void afterThrowingAdvice(JoinPoint joinPoint, Exception exception) {
//        System.out.println("After-throwing..." + exception);
//    }
}